import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { formatToDate, timestampToDate } from '@/utils/dateUtil';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public auctionId = null; // 竞拍活动
  public mapperId = 0; // 对应关系id
  public productId = null; // 产品
  public finalPrice = null; // 最终成交价格
  public numberOfParticipants = 0; // 参与人数
  public bidLog = []; // 出价日志
  public date = ''; // 日期
  public startTime = ''; // 开始时间
  public endTime = ''; // 结束时间
  public status = 0; // 状态

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  auctionId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入竞拍活动',
  },
  productId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入产品',
  },
  date: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入日期',
  },
  startTime: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入开始时间',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'auctionId',
    component: 'NSelect',
    label: '竞拍活动',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择竞拍活动',
      options: dict.getOption('auctionOption'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'productId',
    component: 'NSelect',
    label: '产品',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择产品',
      options: dict.getOption('productOption'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '状态',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择状态',
      options: dict.getOption('auctionLogStatus'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'date',
    component: 'NDatePicker',
    label: '日期',
    defaultValue: new Date(), // 当前时间
    componentProps: {
      // type: 'datetimerange',
      clearable: true,
      // shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '竞拍活动',
    key: 'auctionId',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('auctionOption', row.auctionId);
    },
  },
  {
    title: '产品',
    key: 'productId',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('productOption', row.productId);
    },
  },
  {
    title: '最终成交价格',
    key: 'finalPrice',
    align: 'left',
    width: -1,
  },
  {
    title: '参与人数',
    key: 'numberOfParticipants',
    align: 'left',
    width: -1,
  },
  {
    title: '状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('auctionLogStatus', row.status);
    },
  },
  {
    title: '日期',
    key: 'date',
    align: 'left',
    width: -1,
    render(row: State) {
      return formatToDate(row.date);
    },
  },
  {
    title: '开始时间',
    key: 'startTime',
    align: 'left',
    width: -1,
  },
  {
    title: '结束时间',
    key: 'endTime',
    align: 'left',
    width: -1,
  },
  {
    title: '商品名称',
    key: 'productName',
    align: 'left',
    width: -1,
  },
  {
    title: '商品主图',
    key: 'productMainImage',
    align: 'left',
    width: -1,
    render(row: State) {
      return h('img', {
        src: row.productMainImage,
        style: {
          width: '50px',
          height: '50px',
        },
      });
    },
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['auctionOption', 'productOption', 'auctionLogStatus']);
}
